package com.netdisk.controller;
/**
 * @author chenlong
 * 产生验证码的action
 * 2013-5-17 22:48:41
 */
import java.io.ByteArrayInputStream;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.netdisk.util.RandomPicCreator;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@Component("randomPicAction")
@Scope("prototype")
public class RandomPicAction extends ActionSupport {

	private ByteArrayInputStream inputStream;

	@Override
	public String execute() throws Exception {

		RandomPicCreator pic = RandomPicCreator.Instance();

		this.setInputStream(pic.getImage());// 取得带有随机字符串的图片

		String str = pic.getString().toString();

		HttpSession session =  ServletActionContext.getRequest().getSession();
		session.setAttribute("random", str);// 取得随机字符串放入HttpSession
		Cookie[] c = ServletActionContext.getRequest().getCookies();
		// 设置页面不缓存
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setHeader("PRagma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
		return SUCCESS;
	}

	public void setInputStream(ByteArrayInputStream inputStream) {
		this.inputStream = inputStream;
	}

	public ByteArrayInputStream getInputStream() {
		return inputStream;
	}

}
